package com.zahb.qadx.buriedpoint;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityEvent;
import android.widget.TextView;
import com.google.gson.Gson;
public final class BuriedPoint {
    private static TheBasicParametersBean mTheBasicParametersBean;
    private static TheBasicParametersBean.Properties mProperties;

    /**
     * 埋点数据
     *
     * @param context 上下文
     * @title 标题
     * @element_name Activity
     * @event 提示语
     */

    public static TheBasicParametersBean buriedPoint(Context context, String title, String element_name, String event) {
        if (mTheBasicParametersBean == null) {
            mTheBasicParametersBean = new TheBasicParametersBean();

        }
        mTheBasicParametersBean. setTime(System.currentTimeMillis()+"");
        if (mProperties == null) {
            mProperties = new TheBasicParametersBean.Properties();
            mProperties.setApp_name(getAppName(context));//版本信息
            mProperties.setApp_version(getVersionName(context) + "");//版本信息
        }
        if (title != null) {
            mProperties.setTitle(title);
        }
        mProperties.setElement_name(element_name);
        mTheBasicParametersBean.setProperties(mProperties);
        mTheBasicParametersBean.setEvent(event);
        return mTheBasicParametersBean;
    }


    public static void clickOnThe(View host, int eventType) {
        Context context = host.getContext();
        String path = "";
        if (eventType == AccessibilityEvent.TYPE_VIEW_CLICKED) {
            ViewGroup mViewGroup = (ViewGroup) host.getParent();

            if (mViewGroup.getClass().toString().indexOf("DecorView") != -1) {

            } else {
                String type = determineType(host);
                path = host.getClass().getSimpleName() + type;//点击控件

                traverse(mViewGroup, path);

            }
        }

    }

    //判断控件是否有状态
    private static String determineType(View host) {

        if (host instanceof TextView) {
           String text=  ((TextView) host).getText().toString();
            return text;
        }
        return "无";
    }

    //遍历 父控件路径
    private static void traverse(ViewGroup host, String path) {
        Context context = host.getContext();
        ViewGroup mViewGroup = (ViewGroup) host.getParent();
        if (mViewGroup!=null){
        if (mViewGroup.getClass().toString().indexOf("DecorView") != -1) {
            Gson mGson = new Gson();
//            Log.d("路径", "path:=" + mGson.toJson(buriedPoint(context, path, context.getClass().getSimpleName(), "点击事件")));
        } else {
            path = path + "[0]" + mViewGroup.getClass().getSimpleName();
            traverse(mViewGroup, path);
        }
        }else {

        }
    }

    /**
     * 获取App版本名称
     *
     * @param context 上下文
     * @return 版本名称
     */
    public static String getVersionName(Context context) {
        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_CONFIGURATIONS);
            if (TextUtils.isEmpty(pi.versionName))
                return "";
            else
                return pi.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            Log.e("VersionName", "Exception", e);
            return "";
        }
    }

    /**
     * 获取App版本号
     *
     * @param context 上下文
     * @return 版本号
     */
    public static int getVersionCode(Context context) {
        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_CONFIGURATIONS);
            return pi.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            Log.e("Versioncode", "Exception", e);
            return 1;
        }
    }

    /**
     * 获取App名称
     *
     * @param context 上下文
     * @return 名称
     */
    public static String getAppName(Context context) {
        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_CONFIGURATIONS);
            ApplicationInfo info = pi.applicationInfo;
            return context.getResources().getString(info.labelRes);
        } catch (PackageManager.NameNotFoundException e) {
            Log.e("AppName", "Exception", e);
            return "";
        }
    }
}
